package com.ftg.learn.chapter03;

import com.ftg.learn.chapter03.entity.Dept;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.List;
import java.util.Map;

/**
 * 第一个案例讲解
 *
 * 1、maven 引包 mybatis
 *
 * @author KangJx
 * @date 2021/5/26
 */
public class FirstMybatis {

    public static void main(String[] args) {
        SqlSession ss = null;
        String path = "mybatis-config.xml";
        try {
            Reader reader = Resources.getResourceAsReader(path);
            // ssfb ssf ss
            SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
            SqlSessionFactory ssf = ssfb.build(reader);
            //jdbc getconnection
            ss = ssf.openSession();

            Dept dept = new Dept();
            dept.setDname("齐大实训");
            dept.setLoc("齐齐哈尔");
            dept.setDeptno(91);

            ss.insert("dept.save",dept);
            List<Map<String,Object>> list = ss.selectList("dept.sel",91);
            System.out.println("添加以后的数据===》"+list);

            dept = new Dept();
            dept.setDname("实训一组");
            dept.setLoc("齐齐哈尔大学");
            dept.setDeptno(91);
            ss.update("dept.modify",dept);
            List<Map<String,Object>> list1 = ss.selectList("dept.sel",91);
            System.out.println("修改以后的数据===》"+list1);

            ss.delete("dept.del",dept.getDeptno());


            List<Map<String,Object>> list2 = ss.selectList("dept.sel",91);
            System.out.println("删除以后的数据===》"+list2);

            System.out.println("orm - 把数据 脏数据先放内存里，commit 脏数据落盘  disk");


            List<Map<String,Object>> list3 = ss.selectList("dept.sel1",dept);
            System.out.println(list3);

            //把内存里面的增量数据，落盘(硬盘里)
            ss.commit();

            //绝大多关系型数据库

            //非缓存数据库  (insert update delete 计划 -  cache - commit - 落盘)
            //                                                  rollback


        } catch (IOException e) {
            e.printStackTrace();
            ss.rollback();
        }finally{
            ss.close();
        }



    }

}
